package com.biostime.qdingding.modules.qd.api;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.biostime.qdingding.common.api.FastResponse;
import com.biostime.qdingding.common.api.ListResponse;
import com.biostime.qdingding.common.config.Global;
import com.biostime.qdingding.common.web.BaseController;
import com.biostime.qdingding.modules.cms.service.ArticleService;
import com.biostime.qdingding.modules.qd.api.dto.CourseAlbumDto;
import com.biostime.qdingding.modules.qd.api.dto.ParentGuidanceDto;
import com.biostime.qdingding.modules.qd.service.AlbumService;
import com.biostime.qdingding.modules.qd.service.CourseService;
import com.biostime.qdingding.modules.qd.service.ParentGuidanceService;
import com.github.pagehelper.PageHelper;

/**
 * 发现模块
 * 
 * @author chenweicheng
 * @version 2015-12-10
 */
@RestController
@RequestMapping(value = "${adminPath}/qd/api/discovery")
public class DicoveryApi extends BaseController {
	
	@Autowired
	ArticleService articleService;
	
	@Autowired
	AlbumService albumService;
	
	@Autowired
	CourseService courseService;
	
	@Autowired
	ParentGuidanceService parentGuidance;	

	/**
	 * 发现/课堂相册
	 */
	@RequestMapping(value = "courseAlbum")
	public ListResponse<CourseAlbumDto> courseAlbum(String userId,String centerId,String studentId,
			int pageindex,int pagesize) {		
		String message = null;
		int errCode = 0;	
		List<CourseAlbumDto> albumList = new ArrayList<CourseAlbumDto>();	
		
		if(userId != null && !userId.equals("") && centerId != null && !centerId.equals("")){
			PageHelper.startPage(pageindex, pagesize);
			albumList = albumService.apiCourseAlbum(centerId,studentId);
			
			//设置分享url			
			if(albumList != null && !albumList.isEmpty()){
				for(CourseAlbumDto dto : albumList){
					dto.setUrl("/qdingding-webapp/a/qd/api/discovery/photo"); 
				}				
			}
			
			errCode = 0;
			message= "获取课堂相册列表成功";
		}else{
			errCode = 1;
			message = "获取失败，参数有误";
		}
		
		return FastResponse.create().errCode(errCode).errMsg(message).list(albumList);
	}
	
	/**
	 * 发现/课堂相册
	 */
	@RequestMapping(value = "photo")
	public ModelAndView courseAlbum(String centerId,String studentId,int index) {		
		ModelAndView model = new ModelAndView("modules/qd/photo");		
		List<CourseAlbumDto> albumList = new ArrayList<CourseAlbumDto>();			
		albumList = albumService.apiCourseAlbum(centerId,studentId);
		if(albumList != null && !albumList.isEmpty()){
			
			//图片地址
			String url = "http://"+Global.getConfig("domain")+"/qdingding-webapp/cdn/image/";
			model.addObject("images", albumList.get(index).getImages());		 			
			model.addObject("url", url);
			
		}
		return model;
	}	
	
	/**
	 * 发现/获取父母指南列表
	 */
	@RequestMapping(value = "getGuidanceList")
	public ListResponse<ParentGuidanceDto> getGuidanceList(String userId,String studentId,HttpServletRequest request) {	
		String message = null;
		int errCode = 0;
		List<ParentGuidanceDto>  dtoList = new ArrayList<ParentGuidanceDto>();
	
		if (userId != null && !userId.equals("") && studentId != null && !studentId.equals("")) {		
			dtoList = courseService.apiGetGuidenceList(studentId);
			
			//通过日期主题年龄段系列查找文章
			if(dtoList != null && !dtoList.isEmpty()){
				for(ParentGuidanceDto  dto : dtoList){
					String serial = dto.getSerial();
					String theme = dto.getTheme();
					String clevel = dto.getClevel();
					String name = clevel+serial+theme;
					dto.setUrl(parentGuidance.getParentGuidanceUrl(request, name)); 
				}
			}
			message = "获取父母指南列表成功";
			errCode = 0;
			
		} else {
			message = "获取父母指南列表失败，参数有误";
			errCode = 1;
		}
		
		return FastResponse.create().errMsg(message).errCode(errCode).list(dtoList);
	}		
}
